package com.medical.data.service.impl;

import com.medical.data.domain.Upload;
import com.medical.data.domain.User;
import com.medical.data.mapper.UploadMapper;
import com.medical.data.service.TokenSerivce;
import com.medical.data.service.UploadService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.medical.data.utils.AddressUtils;
import com.medical.data.utils.FileUtils;
import com.medical.data.utils.HttpUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author cj
 * @since 2024-02-14
 */
@Service
public class UploadServiceImpl extends ServiceImpl<UploadMapper, Upload> implements UploadService {

    @Autowired
    private FileUtils fileUtils;

    @Autowired
    private TokenSerivce tokenSerivce;

    @Override
    public String uploadImage(MultipartFile file, String formId, HttpServletRequest request,String name) {
        User user = tokenSerivce.getUser();
        String url = fileUtils.uploadImage(file,name);
        Upload upload = new Upload().setUrl(url)
                .setIp(AddressUtils.getRealAddressByIP(HttpUtils.getIpAddr(request)))
                .setFormId(formId)
                .setUserId(user.getId());
        baseMapper.insert(upload);
        return url;
    }
}
